// Running the following code before any other code will create String.trim if it's not natively available.
if (!String.prototype.trim) {  
  String.prototype.trim = function () {  
    return this.replace(/^\s+|\s+$/g,'');  
  };  
};

util = {
    /**
     * Returns a random integer that is uniformly distributed between min and max
     * 
     * @param {number} min
     * @param {number} max
     * @return {number}
     */
  randomInt: function ( min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },
  /**
   * Gets the user's preferred language from the browser settings 
   * 
   * @return {string}
   */
  getUserLanguage: function () {
    var lang = window.navigator.userLanguage || window.navigator.language;
    return lang.substring(0,2);
  },
  languageNames: { "de":"Deutsch", "en":"English", "es":"Español", "fr":"Français", 
    "pt":"Português", "ru":"Русский", "zh":"中文"
  },
/**
 * Verifies if a value represents an integer
 * @param {number} x
 * @return {boolean}
 */
  isInteger: function (x) {
    return typeof(x) === "number" && x.toString().search(/^-?[0-9]+$/) == 0;
  },
  /**
   * Verifies if a value represents a non-negative integer
   * @param {number} x
   * @return {boolean}
   */
  isNonNegativeInteger: function (x) {
    return util.isInteger(x) && x >= 0;
  },
 /**
  * Verifies if a value represents a positive integer
  * @param {number} x
  * @return {boolean}
  */
  isPositiveInteger: function (x) {
    return util.isInteger(x) && x > 0;
  },
/**
 * Checks if a string contains an arithmetic operator 
 * (hence representing an arithmetic expression)
 * 
 * @param {string} s  The string to be checked
 */
  containsArithmeticOperator: function (s) {
    for (var i=0; i < oa.view.operatorSymbols.length; i++) {
      if (s.indexOf(oa.view.operatorSymbols[i]) != -1) return true;
    }
  },
  /**
   * Creates a random permutation of an array
   * 
   * @param {array} a
   */
    shuffle: function (a) {
      var tmp, current, top = a.length;
      if (top>1) {
        while(--top) {
          current = Math.floor(Math.random() * (top + 1));
          tmp = a[current];
          a[current] = a[top];
          a[top] = tmp;
        }
      }
      return a;
    },
/**
 * Returns a string representation of an object
 * Useful for debugging
 * 
 * @param {object} o
 * @return {string}
 */
  objectToString: function (o) {
    var t = "";
    var s = "Object: ";
    for (var p in o) {
      if (o[p] == null) t = "null";
      else if (typeof(o[p]) === "object") t = util.objectToString(o[p]);
      else if (typeof(o[p]) !== "function") t = p + " = " + o[p] + "  ";
      s = s+t;
    }
  return s;
  }, 
  /**
  *Returns the maxim number from an array
  *@param arrayList the list of an array
  */
  getMaxFromArray: function (arrayList) {
	  var maxNumber = arrayList[0];
	  for (i=1, max = arrayList.length; i < max; i+=1) {
		  if (maxNumber <= arrayList[i]) {
			  maxNumber = arrayList[i];
		  }
	  }
    return maxNumber;
  },
  /**
  *Parse a string, verify if the array has numbers, and then returns the new array with numbers only
  *@param stringToParse the string that may have within numbers
  *@param radx the a number from 2 to 36 that represents the numeral system to be used
  *@return arrayNumberResult
  */
  parseStringToNumber:function(stringToParse,radx){
	var arrayNumberResult = new Array;
	var contor = 0;
	for(var i=0, max = stringToParse.length; i < max; i+=1){
		if(this.isInteger(parseInt(stringToParse[i],10))){
			arrayNumberResult[contor++] = stringToParse[i];
			}
	}
	return arrayNumberResult;
  },
  /**
  *Retrieves a specified node from a given list
  *@param nodeList 
  *@param nameOfNode 
  *@return {node} the desired node
  */
  findNodeInNodeList:function(nodeList,nameOfNode){
	var copyNodeList = nodeList;
	if(copyNodeList.hasChildNodes()){
		while(copyNodeList.hasChildNodes())
		{
			if(copyNodeList.firstChild.nodeName===nameOfNode){
				return copyNodeList.firstChild;
			}else{
			copyNodeList.removeChild(copyNodeList.firstChild);}
		}
	}
	return false;
  }
};
